package cn.iocoder.yudao.module.system.dal.mysql.erp.warehouseinventorysku;
import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.module.system.controller.admin.erp.warehouseinventorysku.vo.WarehouseInventorySkuPageReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.erp.warehouseinventorysku.WarehouseInventorySkuDO;
import org.apache.ibatis.annotations.Mapper;

/**
 * 仓库库存sku Mapper
 *
 * @author 芋道源码
 */
@Mapper
public interface WarehouseInventorySkuMapper extends BaseMapperX<WarehouseInventorySkuDO> {

    default PageResult<WarehouseInventorySkuDO> selectPage(WarehouseInventorySkuPageReqVO reqVO) {
        LambdaQueryWrapperX<WarehouseInventorySkuDO> query = new LambdaQueryWrapperX<WarehouseInventorySkuDO>()
        .betweenIfPresent(WarehouseInventorySkuDO::getCreateTime, reqVO.getCreateTime())
//                .eqIfPresent(WarehouseInventorySkuDO::getInventoryId, reqVO.getInventoryId())
        .eqIfPresent(WarehouseInventorySkuDO::getSku, reqVO.getSku())
        .eqIfPresent(WarehouseInventorySkuDO::getNum, reqVO.getNum())
        .eqIfPresent(WarehouseInventorySkuDO::getLength, reqVO.getLength())
        .eqIfPresent(WarehouseInventorySkuDO::getWidth, reqVO.getWidth())
        .eqIfPresent(WarehouseInventorySkuDO::getHeight, reqVO.getHeight())
        .eqIfPresent(WarehouseInventorySkuDO::getWeight, reqVO.getWeight())
        .eqIfPresent(WarehouseInventorySkuDO::getWarehouseId, reqVO.getWarehouseId())
        .eqIfPresent(WarehouseInventorySkuDO::getCustomerId, reqVO.getCustomerId())
        .likeIfPresent(WarehouseInventorySkuDO::getSku, reqVO.getSku());
        if(StrUtil.isNotBlank(reqVO.getWarehouse())){
        query.apply("(SELECT COUNT(0)>0 FROM erp_warehouse w WHERE w.id=t.customer_id and w.deleted=0 AND w.name LIKE '%"+reqVO.getWarehouse()+"%')");
        }
        if(StrUtil.isNotBlank(reqVO.getCustomer())){
        query.apply("(SELECT COUNT(0)>0 FROM system_users u WHERE u.id=t.customer_id and u.deleted=0 AND u.nickname LIKE '%"+reqVO.getCustomer()+"%')");
        }
         query.orderByDesc(WarehouseInventorySkuDO::getId);
        return selectPage(reqVO, query);
    }

}